//
//  MyFadeBackViewController.m
//  FD
//
//  Created by Yasin on 2018/10/17.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MyFadeBackViewController.h"
#import "UIButton+Extension.h" //快速创建一个按钮
#import "PlaceholderTextView.h" //2019-2-19 有毒
//#import "YYTextView.h"


@interface MyFadeBackViewController ()<UITextViewDelegate>
///**意见反馈输入框 2019-2-19有毒*/
@property (nonatomic, strong) PlaceholderTextView *textView;
/**意见反馈输入框 YYTextView*/
//@property (nonatomic, strong) YYTextView *textView;
/**意见反馈*/
//@property (nonatomic, strong) UITextView *textView;

/**提交*/
@property (nonatomic,  strong) UIButton *tijiaoBtn;
@end

@implementation MyFadeBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupView];
}

- (void)setupView {
    self.view.backgroundColor = [UIColor whiteColor];
    NSLocalizedString(My_FeedBack, @"意见反馈");
    self.navigationItem.title = NSLocalizedString(My_FeedBack, @"意见反馈");
    [self.view addSubview:self.tijiaoBtn];
    self.view.backgroundColor = kMainLightGrayColor;

    self.textView.frame = CGRectMake(0, FD_NavH, SCREEN_WIDTH, 180);
    [self.view addSubview:self.textView];
    
    self.tijiaoBtn.frame = CGRectMake(20, 300, SCREEN_WIDTH - 40, 50);
    [self.view addSubview:self.tijiaoBtn];
}

#pragma mark - getter
//- (YYTextView *)textView {
//    if (!_textView) {
//        _textView = [[YYTextView alloc] initWithFrame:CGRectMake(20, 84, SCREEN_WIDTH, 180)];
//        _textView.font = [UIFont systemFontOfSize:16.0f];
//        _textView.placeholderText = NSLocalizedString(L_PJ_QinShuRuYiJianFanKui, @"请输入意见反馈");
//        _textView.placeholderTextColor = [UIColor lightGrayColor];
//        _textView.backgroundColor = [UIColor whiteColor];
//
//    }
//    return _textView;
//}

-(PlaceholderTextView *)textView{

    if (!_textView) {
        _textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(20, 84, self.view.frame.size.width - 40, 180)];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:16.0f];
        _textView.textColor = [UIColor blackColor];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.editable = YES;
        _textView.placeholderColor = [UIColor lightGrayColor];
        _textView.placeholder = NSLocalizedString(L_PJ_QinShuRuYiJianFanKui, @"请输入意见反馈");
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return _textView;
}

//- (UITextView *)textView {
//    if (!_textView) {
//        NSTextContainer *containte = [[NSTextContainer alloc] initWithSize:CGSizeMake(SCREEN_WIDTH, 180)];
//        _textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 84, SCREEN_WIDTH-40, 180)];
//        _textView.text = @"这个需求很扯淡";
//        _textView.layer.borderWidth = 1.0;
//        _textView.layer.borderColor = [UIColor blackColor].CGColor;
////        _textView.automaticallyAdjustsScrollViewInsets = NO;
//        self.automaticallyAdjustsScrollViewInsets = NO;
//    }
//    return _textView;
//}


- (UIButton *)tijiaoBtn {
    if (!_tijiaoBtn) {
        _tijiaoBtn = [UIButton createFastRedBtnRadius:4 target:self action:@selector(tiJiaoBtnAction:) title:NSLocalizedString(Submit, @"提交")];
        _tijiaoBtn.backgroundColor = [UIColor rb_colorHex:0xE54B4B]; //红色
    }
    return _tijiaoBtn;
}


#pragma mark - 代理 UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}


#pragma mark - 点击事件
- (void)tiJiaoBtnAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    
    //调接口
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *fadebackStr = self.textView.text;
    
    [FDHttpAPi requestUserFadebackWithContent:fadebackStr successBlock:^(id responseObjc) {
        
        [hud showAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else if ([event isEqualToString:@"ERROR"]){
            [JRToast showWithText:responseObjc[@"describe"]];
        }
    } failBlock:^(NSError *error) {
        [hud hideAnimated:YES];
        FDLog(@"%@",[error localizedDescription]);
    }];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
}

@end
